package com.lkd.job;

import com.lkd.entity.UserEntity;
import com.lkd.service.UserService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class CreateZsetJob {
    @Autowired
    private UserService userService;

    @Autowired
    private RedisTemplate redisTemplate;

    @XxlJob("createSetJobHandler")
    public ReturnT<String> workCountInitJobHandler(String param) throws Exception{
        //todo:查询用户列表
        int paramDay = 1;
        if("today".equals(param)){
            paramDay = 0;
        }
        String date = LocalDateTime.now().plusDays(paramDay).format(DateTimeFormatter.ofPattern("yyyyMMdd"));
        List<UserEntity> list = userService.list();
        for (UserEntity entity : list) {
            if(entity.getRoleId()!=1) {
                Long regionId = entity.getRegionId();
                String roleCode = entity.getRoleCode();
                String key = "taskNumSet." + date + regionId + roleCode;
                //todo:构建数据（zset）
                redisTemplate.opsForZSet().add(key, entity.getId(), 0);
                redisTemplate.expire(key, 1, TimeUnit.DAYS);
            }
        }
        return ReturnT.SUCCESS;
    }
}
